package cn.flying.cloud.oauth.server.configuration.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

import cn.flying.cloud.base.common.enums.RtCodeEnum;
import cn.flying.cloud.base.common.vo.Rt;

/**
 * OAuth处理异常翻译转换，统一将其他的异常类型转换为OAuth2Exception
 *
 * @author: admin
 * @date: 2024年04月30日 22:11
 * @version: 1.0
 */
public class CustomWebResponseExceptionTranslator implements WebResponseExceptionTranslator {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public ResponseEntity<Rt> translate(Exception e) throws Exception {
        logger.error("认证异常", e);

        return new ResponseEntity<>(Rt.error(RtCodeEnum.R10020), HttpStatus.UNAUTHORIZED);
    }
}
